﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using EluZSProvider;
using Antu.Core.Services;
using System.Collections;
using System.Web.UI.WebControls;

namespace WebSys.UI
{
    public partial class ZSPG_PGALQuery : System.Web.UI.Page
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        #region 属性
        protected global::System.Web.UI.WebControls.DropDownList txtdistrict;
        protected global::System.Web.UI.WebControls.DropDownList txtjylx;
        protected global::System.Web.UI.WebControls.DropDownList txtjb;
        protected global::System.Web.UI.WebControls.DropDownList txtsfct;
        protected global::System.Web.UI.WebControls.DropDownList ddlfwyt;
        protected global::Antu.WebControls.MPCalendar txtdatestart;
        protected global::Antu.WebControls.MPCalendar txtdateend;

        protected global::System.Web.UI.WebControls.TextBox txtbuildareastart;
        protected global::System.Web.UI.WebControls.TextBox txtbuildareaend;
        protected global::System.Web.UI.WebControls.TextBox txtallmoneystart;
        protected global::System.Web.UI.WebControls.TextBox txtallmoneyend;
        protected global::System.Web.UI.WebControls.TextBox txtmoneystart;
        protected global::System.Web.UI.WebControls.TextBox txtmoneyend;

        protected global::System.Web.UI.WebControls.TextBox txtlocation;
        protected global::System.Web.UI.WebControls.TextBox txthillno;
        protected global::System.Web.UI.WebControls.TextBox txtrightno;
        protected global::System.Web.UI.WebControls.TextBox txtcb;

        protected global::System.Web.UI.WebControls.TextBox txtjylb;
        protected global::System.Web.UI.WebControls.TextBox txtzrlb;
        protected global::System.Web.UI.WebControls.TextBox txtcqqwjb;

        protected global::System.Web.UI.WebControls.Literal Literal1;
        protected global::Antu.WebControls.CustomGridView CustomGridView1;
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDll();
                BindData();
            }
        }
        protected void BindDll() 
        {
            txtdistrict.DataSource = _EluZSDataProvider.GetTableNeto2("Select keyvalue From Sys_Params Where name='区属'");
            txtdistrict.DataTextField = "keyvalue";
            txtdistrict.DataValueField = "keyvalue";
            txtdistrict.DataBind();
            txtjylx.DataSource = _EluZSDataProvider.GetTableNeto2("Select keyvalue From Sys_Params Where name='交易类型'");
            txtjylx.DataTextField = "keyvalue";
            txtjylx.DataValueField = "keyvalue";
            txtjylx.DataBind();
            //txtjb.DataSource = _EluZSDataProvider.GetTableNeto2("Select keyvalue From Sys_Params Where name='级别'");
            //txtjb.DataTextField = "keyvalue";
            //txtjb.DataValueField = "keyvalue";
            //txtjb.DataBind();
            //txtsfct.DataSource = _EluZSDataProvider.GetTableNeto2("Select keyvalue From Sys_Params Where name='是否成套'");
            //txtsfct.DataTextField = "keyvalue";
            //txtsfct.DataValueField = "keyvalue";
            //txtsfct.DataBind();
        }
        protected void BindData()
        {
            string strSql = string.Format(@"Select t.Id,t.District 区属,t.Location 房屋坐落,t.District_Grade 征收区位级别,Round(t.Archit_Area, 2) 建筑面积,t.Sign_Date 交易日期,Round(t.Trade_Price, 2) 申报交易单价,Round(t.Trade_Total, 2) 申报交易总额 From Cq_Cam_Case t Where 1 = 1 ");
            string strWhere = GetSqlWhere();
            if (!string.IsNullOrEmpty(strWhere))
            {
                strSql += strWhere;
            }
            strSql += " And Rownum<1001 ";
            DataTable dt = _EluZSDataProvider.GetTableNeto2(strSql);
            if (dt.Rows.Count <=0)
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "disspan", "<script>disspan1();</script>");
            }
            CustomGridView1.DataSource = dt;
            CustomGridView1.RecordCount = dt.Rows.Count;
            CustomGridView1.PageSize = 10;
            CustomGridView1.DataBind();
            //BindTotal(dt);
        }
        protected void BindTotal(DataTable dt)
        {
            double buildarea = 0;
            double reportprice = 0;
            double reportallmoney = 0;
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    buildarea += Convert.ToDouble(dt.Rows[i]["建筑面积"]);
                    reportallmoney += Convert.ToDouble(dt.Rows[i]["申报交易总额"]);
                }
            }
            reportprice = Math.Round(reportallmoney / buildarea, 2);
            Literal1.Text = "申报交易单价平均值：<span style='color:Red'>" + reportprice.ToString() + "</span>元　　　建筑面积合计：<span style='color:Red'>" + Convert.ToString(Math.Round(buildarea, 2)) + "</span>平方米　　　申报交易总额合计：<span style='color:Red'>" + Convert.ToString(Math.Round(reportallmoney, 2)) + "</span>元";
        }
        protected string GetSqlWhere() 
        {
            string strWhere = string.Empty;
            if (!string.IsNullOrEmpty(txtbuildareastart.Text.Trim()))
            {
                strWhere += string.Format(" and ARCHIT_AREA>={0}", txtbuildareastart.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtbuildareaend.Text.Trim()))
            {
                strWhere += string.Format(" and ARCHIT_AREA<={0}", txtbuildareaend.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtallmoneystart.Text.Trim()))
            {
                strWhere += string.Format(" and trade_total>={0}", txtallmoneystart.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtallmoneyend.Text.Trim()))
            {
                strWhere += string.Format(" and trade_total<={0}", txtallmoneyend.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtmoneystart.Text.Trim()))
            {
                strWhere += string.Format(" and TRADE_PRICE>={0}", txtmoneystart.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtmoneyend.Text.Trim()))
            {
                strWhere += string.Format(" and TRADE_PRICE<={0}", txtmoneyend.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtdatestart.Text.Trim()))
            {
                strWhere += string.Format(" and SIGN_DATE>=to_date('{0}','yyyy-mm-dd')", txtdatestart.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtdateend.Text.Trim()))
            {
                strWhere += string.Format(" and SIGN_DATE<=to_date('{0}','yyyy-mm-dd')", txtdateend.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtdistrict.SelectedValue))
            {
                strWhere += string.Format(" and district='{0}'", txtdistrict.SelectedValue);
            }
            if (!string.IsNullOrEmpty(txtlocation.Text.Trim()))
            {
                strWhere += string.Format(" and location like '%{0}%'", txtlocation.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txthillno.Text.Trim()))
            {
                strWhere += string.Format(" and HILL_NO like '%{0}%'", txthillno.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtrightno.Text.Trim()))
            {
                strWhere += string.Format(" and RIGHT_NO like '%{0}%'", txtrightno.Text.Trim());
            }
            //if (hsQuery["txtcb"].ToString() != "")
            //{
            //    strWhere += string.Format(" and RIGHT_NO like '%{0}%'", hsQuery["txtcb"]);
            //}
            //if (hsQuery["txtsfct"].ToString() != "")//txtsfct.SelectedValue
            //{
            //    strWhere += string.Format(" and RIGHT_NO like '%{0}%'", hsQuery["txtsfct"]);
            //}
            //if (hsQuery["txtjb"].ToString() != "")//txtjb.SelectedValue
            //{
            //    strWhere += string.Format(" and DISTRICT_GRADE ='{0}'", hsQuery["txtjb"]);
            //}
            if (!string.IsNullOrEmpty(txtjylx.SelectedValue))
            {
                strWhere += string.Format(" and TRADE_TYPE='{0}'", txtjylx.SelectedValue);
            }
            if (!string.IsNullOrEmpty(txtjylb.Text.Trim()))
            {
                strWhere += string.Format(" and RIGHT_TYPE='{0}'", txtjylb.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtzrlb.Text.Trim()))
            {
                strWhere += string.Format(" and RIGHT_SOURCE='{0}'", txtzrlb.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtcqqwjb.Text.Trim()))
            {
                strWhere += string.Format(" and DISTRICT_GRADE='{0}'", txtcqqwjb.Text.Trim());
            }
            if (!string.IsNullOrEmpty(ddlfwyt.SelectedValue))
            {
                strWhere += string.Format(" and houseuse_type='{0}'", ddlfwyt.SelectedValue);
            }
            return strWhere;
        }
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindData();
        }
        protected void CustomGridView1_OnLoadData(object sender, EventArgs e)
        {
            BindData();
        }
        protected void CustomGridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.RowType)
            {
                case DataControlRowType.Header:
                    TableHeaderCell head = new TableHeaderCell();
                    head.Text = "详细";
                    e.Row.Cells.Add(head);
                    break;
                case DataControlRowType.DataRow:
                    string id = "";
                    DataRowView rowv = e.Row.DataItem as DataRowView;
                    if (rowv != null)
                    {
                        id = rowv["id"].ToString();
                    }
                    HyperLink hyDetail = new HyperLink();
                    hyDetail.Text = "详细";
                    hyDetail.NavigateUrl = "#";
                    hyDetail.Attributes.Add("onclick", string.Format("OpenModelWindow('{0}')", id));
                    TableCell cell = new TableCell();
                    cell.Controls.Add(hyDetail);
                    e.Row.Cells.Add(cell);
                    break;
            }
        }
    }
}